package com.shop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.common.ResponseResult;
import com.shop.entity.Manage;
import com.shop.entity.vo.ManageRoleVo;
import com.shop.service.manageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class manageController {
    @Autowired
    private manageService manageService;
    @GetMapping("/user")
    public ResponseResult select(){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setSuccess("成功",manageService.selectAll());
        return responseResult;
    }
    @GetMapping("/users")
    public ResponseResult selectPage(@RequestParam(required = false)String query,
                                     @RequestParam(required = false)Integer pagenum,
                                     @RequestParam(required = false)Integer pagesize){
        ResponseResult result = new ResponseResult();
//        Page<Manage> page = manageService.selectPage(pagenum,pagesize);
        Page<ManageRoleVo> list = manageService.selectVo(pagenum,pagesize,query);
        result.setSuccess("查询成功",list);
        return result;
    }
    @PostMapping("/users")
    public ResponseResult addUser(@RequestBody Manage manage){
        return manageService.addManage(manage);
    }
    @DeleteMapping("/users")
    public ResponseResult deleteUser(@RequestParam(required = true)Integer id){
        return manageService.deleteManage(id);
    }
    @PutMapping("/users/{uId}/state/{type}")
    public ResponseResult updateUser(@PathVariable Integer uId,@PathVariable Boolean type){
        return manageService.updateManage(uId,type);
    }
    @GetMapping("/users/{id}")
    public ResponseResult selectOne(@PathVariable Integer id){
        return manageService.selectOne(id);
    }
    @PutMapping("/users/{id}")
    public ResponseResult updateOne(@PathVariable Integer id,@RequestBody Manage manage){
        return manageService.updateOne(id, manage);
    }
    @PutMapping("/users/{id}/role")
    public ResponseResult updateRole(@PathVariable Integer id,@RequestBody Map map){
        ResponseResult result = new ResponseResult();
        try {
            int rid= Integer.parseInt(map.get("rid").toString());
            return manageService.updateRole(id,rid);
        }catch (Exception e){
            result.setBADREQUEST();
            return result;
        }
    }


}
